using B.S.Data.MES.API.Applications.B.S.KApplications.Command.InvoiceCommand;
    using B.S.Data.MES.API.Applications.B.S.KApplications.Command.InvoiceReceiptCommand;
using B.S.Data.MES.API.Applications.B.S.KApplications.Query.InvoiceQuery;
using B.S.Data.MES.API.Applications.B.S.KApplications.Query.InvoiceReceiptQuery;
using B.S.Data.MES.API.Applications.Dingdan;
using B.S.Data.MES.API.Dto.InvoiceDTO;
using B.S.Domain.Ticket;
using B.S.Domain.基础数据.客户管理;
using B.S.Requst;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Controllers
{
    /// <summary>
    /// 销售发票控制器
    /// 处理销售发票相关的HTTP请求
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SalesInvoiceController : ControllerBase
    {
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// 注入MediatR依赖
        /// </summary>
        /// <param name="mediator">MediatR实例</param>
        public SalesInvoiceController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 添加销售发票
        /// </summary>
        /// <param name="command">添加销售发票命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddSalesInvoice([FromBody] AddSalesInvoiceCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 更新销售发票
        /// </summary>
        /// <param name="command">更新销售发票命令</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<APIRequst<bool>> UpdateSalesInvoice([FromBody] UpdateSalesInvoiceCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除销售发票
        /// </summary>
        /// <param name="command">删除销售发票命令</param>
        /// <returns>操作结果</returns>
        [HttpPost("Delete")]
        public async Task<APIRequst<bool>> DeleteSalesInvoice([FromBody] DeleteSalesInvoiceCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取销售发票列表（分页）
        /// </summary>
        /// <param name="query">获取销售发票查询</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<APIRequst<APIPageing<System.Collections.Generic.List<B.S.Data.MES.API.Dto.InvoiceDTO.SalesInvoiceDTO>>>> GetSalesInvoiceList([FromQuery] GetSalesInvoiceQuery query)
        {
            return await _mediator.Send(query);
        }

        /// <summary>
        /// 获取销售发票详情
        /// </summary>
        /// <param name="invoiceId">发票ID</param>
        /// <returns>发票详情</returns>
        [HttpGet]
        public async Task<APIRequst<B.S.Data.MES.API.Dto.InvoiceDTO.SalesInvoiceDTO>> GetSalesInvoiceDetail(int invoiceId)
        {
            return await _mediator.Send(new GetSalesInvoiceDetailQuery { InvoiceId = invoiceId });
        }

        /// <summary>
        /// 获取所有销售发票列表（无条件）
        /// </summary>
        /// <returns>所有销售发票列表</returns>
        [HttpGet]
        public async Task<APIRequst<List<B.S.Data.MES.API.Dto.InvoiceDTO.SalesInvoiceDTO>>> GetAllSalesInvoiceList()
        {
            return await _mediator.Send(new GetAllSalesInvoiceQuery());
        }
    
        [HttpGet]
        public async Task<APIRequst<List<DingDan>>> DingdanQuery([FromQuery]dingdanQueryCommand request, CancellationToken cancellationToken)
        {
          return await _mediator.Send(request);
        }


        /// <summary>
        /// 客户查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIRequst<List<client>>> clietQuery([FromQuery]clientQueryCommand request, CancellationToken cancellationToken)
        {
          return _mediator.Send(request);
        }

        #region 到票信息管理

        /// <summary>
        /// 添加到票信息
        /// </summary>
        /// <param name="command">添加到票信息命令</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddInvoiceReceipt([FromBody] AddInvoiceReceiptCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 更新到票信息
        /// </summary>
        /// <param name="command">更新到票信息命令</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<APIRequst<bool>> UpdateInvoiceReceipt([FromBody] UpdateInvoiceReceiptCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除到票信息
        /// </summary>
        /// <param name="command">删除到票信息命令</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<APIRequst<bool>> DeleteInvoiceReceipt([FromQuery] DeleteInvoiceReceiptCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取到票列表（分页）
        /// </summary>
        /// <param name="query">获取到票列表查询</param>
        /// <returns>分页查询结果</returns>
        [HttpGet]
        public async Task<APIRequst<APIPageing<List<InvoiceReceiptDTO>>>> GetInvoiceReceiptList([FromQuery] GetInvoiceReceiptQuery query)
        {
            return await _mediator.Send(query);
        }

        /// <summary>
        /// 获取到票详情
        /// </summary>
        /// <param name="receiptId">到票记录ID</param>
        /// <returns>到票详情</returns>
        [HttpGet]
        public async Task<APIRequst<InvoiceReceiptDTO>> GetInvoiceReceiptDetail(int receiptId)
        {
            return await _mediator.Send(new GetInvoiceReceiptDetailQuery { ReceiptId = receiptId });
        }

        #endregion
    }
}